# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# To build with Qt-Creator:
# 1. Launch Qt-Creator with the env (set from cmd.exe):
# set "MINGW_PREFIX=C:/msys64/mingw64"
# set "ACLOCAL_PATH=%MINGW_PREFIX%/share/aclocal:/usr/share/aclocal"
# set "PKG_CONFIG_PATH=%MINGW_PREFIX%/lib/pkgconfig"
# set "PATH=%MINGW_PREFIX%/bin;%PATH%"
# 2. Specify the following as the CMake arguments:
# -DMINGW_LIBS=%MINGW_PREFIX% -DCMAKE_BUILD_TYPE=Debug -DWITH_SYSTEM_GLEW=ON -DWITH_SYSTEM_OPENJPEG=ON -DWITH_INSTALL_PORTABLE=OFF -DWITH_OPENCOLLADA=ON -DWITH_GAMEENGINE=ON -DWITH_RAYOPTIMIZATION=ON -DWITH_JACK=OFF -DWITH_PLAYER=ON -DWITH_CODEC_FFMPEG=ON -DWITH_CODEC_SNDFILE=ON -DWITH_CYCLES=ON -DWITH_CYCLES_CUDA_BINARIES=OFF -DWITH_CYCLES_OSL=ON -DWITH_FFTW3=ON -DWITH_MOD_OCEANSIM=ON -DPYTHON_VERSION=3.7 -DPYTHON_LIBPATH=%MINGW_PREFIX%/lib -DPYTHON_LIBRARY="python3.6m" -DPYTHON_INCLUDE_DIRS=/msys64/mingw64/include/python3.6m -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_INSTALL_NUMPY=OFF -DPYTHON_NUMPY_PATH=%MINGW_PREFIX%/lib/python3.6/site-packages -DCYCLES_OSL=%MINGW_PREFIX% -DWITH_IMAGE_REDCODE=ON -DWITH_OPENMP=ON -DWITH_LLVM=ON -DLLVM_STATIC=ON

_realname=blender
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.0.2
pkgrel=12
pkgdesc="A fully integrated 3D graphics creation suite (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.blender.org"
msys2_repository_url="https://projects.blender.org/blender/blender"
msys2_references=(
  'archlinux: blender'
  'cpe: cpe:/a:blender:blender'
)
license=('spdx:GPL-2.0-or-later')
# build conflict with "${MINGW_PACKAGE_PREFIX}-glog"
depends=("${MINGW_PACKAGE_PREFIX}-alembic"
         #"${MINGW_PACKAGE_PREFIX}-audaspace"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         #"${MINGW_PACKAGE_PREFIX}-bullet-dp"
         #"${MINGW_PACKAGE_PREFIX}-draco" It builds an internal library
         "${MINGW_PACKAGE_PREFIX}-embree"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         #"${MINGW_PACKAGE_PREFIX}-gflags"
         #"${MINGW_PACKAGE_PREFIX}-glog"
         "${MINGW_PACKAGE_PREFIX}-imath"
         #"${MINGW_PACKAGE_PREFIX}-jemalloc"
         "${MINGW_PACKAGE_PREFIX}-libepoxy"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         #"${MINGW_PACKAGE_PREFIX}-materialx"
         #"${MINGW_PACKAGE_PREFIX}-usd"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencollada"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-openimagedenoise")
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openpgl"
         "${MINGW_PACKAGE_PREFIX}-openshadinglanguage"
         "${MINGW_PACKAGE_PREFIX}-opensubdiv"
         "${MINGW_PACKAGE_PREFIX}-openvdb"
         "${MINGW_PACKAGE_PREFIX}-openxr-sdk"
         "${MINGW_PACKAGE_PREFIX}-potrace"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-wintab-sdk"
             $([[ "$CARCH" == "aarch64" ]] && echo "${MINGW_PACKAGE_PREFIX}-sse2neon"))
install=blender-${MSYSTEM}.install
source=(https://download.blender.org/source/${_realname}-${pkgver}.tar.xz
        blender-addons-${pkgver}.tar.gz::https://projects.blender.org/blender/blender-addons/archive/v${pkgver}.tar.gz
        0001-mingw-buildsystem.patch
        0002-fix-mingw-w64-definitions.patch
        0003-disable-static_assert_align-on-mingw.patch
        0004-fix-build-with-openpgl-0.6.patch::https://projects.blender.org/blender/blender/pulls/118328.patch
        0005-fix-WINTAB_PRINTF-on-mingw.patch
        0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
        0007-disable-compiler-warnings.patch
        0008-mingw-use-X11-like-path-layout.patch
        0009-include-missing-headers.patch
        0010-llvm-no-longer-needed.patch::https://projects.blender.org/blender/blender/commit/088ae8d9.patch
        0011-no-TIME_UTC-on-msvcrt.patch
        0012-fix-build-with-OSL-1.13.patch::https://projects.blender.org/blender/blender/commit/798a0b30.patch
        0013-force-draco.patch
        0014-fix-find-numpy-2.patch
        0015-mingw-python-like-msvc.patch
        0016-mingw-windres.patch
        0017-fix-build-with-python-3.12.patch::https://github.com/blender/blender/commit/252ae702.patch
        0018-fix-work-with-openvdb-11.patch::https://projects.blender.org/blender/blender/commit/bbb7e7a6.patch
        0019-fix-casts.patch
        0020-blendthumb-build.patch
        0021-fix-build-with-openvdb-12.patch::https://projects.blender.org/blender/blender/commit/43326458.patch
        0022-find-llvm-convert-paths.patch
        0023-link-errors.patch
        0024-openvdb-shared.patch
        0025-callback-function-prototype-fix.patch
        0028-use-std-namespace.patch
        0032-fix-finding-tbb.patch
        0033-dont-add-fno-rtti-compile-option.patch
        0034-Fix-ARM64-Build.patch
        0035-Enable-SSE2NEON-on-Windows-ARM.patch
        0039-no-WRL-on-mingw-w64.patch
        ffmpeg-7-1.patch
        ffmpeg-7-2.patch)
sha256sums=('aaa0e729da7591cfbf45772af76345977daaa7b11a0af35d98f9313e246077a3'
            '16e9a9845e0db4e59d3be40f18dc631ab834f418cb226433947a90323e7c847b'
            'ee9b8f5c191a59960682729d89f7c51e3c9bf50992d6b86f933d05b98e4c5a72'
            '11fd854c62dc276f3edace85e285764506091e9203abf7c90dcad98005e9cfc7'
            '891c19119b5cb6ff05b93ed8183841593bc20444dd4070bc5c69a701d857e030'
            'de81f189dcef9ad0d46c57f16bc618751d6e749765cfb4d8e9c132ffcbd86c1d'
            '2cd8eea86a67a330d8eadb09ec1bafbe99a4c910654aa65dc1c6389b1e6ae678'
            '1ccbf571e8dae23d0bff4ac848825788ac0a838261c5b02578c30852061238c8'
            '94b1299ff8fff061e1387e5ed14b4c1c58ae834c30c3360a0c53aaadf226064f'
            '9822ab6ab47fb67e8a75df44c3a7c3cdd060ad0c63acb87eeeea0be53bb0f252'
            '05aabd1aef3f0894889117dafc51e9d63929eb64b22fc5790a066850f8fe7388'
            '6b8a01acb726b984960d95ce7da5b69204b2d80ebaa6c2c5a7a87924ea26b986'
            'e018b7ee07f7b8b4964fd30feb0495101d0a6b51f218652d12ae2b6369d495eb'
            '44879c20e31e60ebe3232f0be9d808b343cf986f7a0fc6df53a90ac270ab42be'
            '6b08689f6c050780feebee330ad7c40cc528556ef80cb817e1551b11123edd13'
            '06444eeb84d61437527d402c376024a862e60766963273868d76c604185829d1'
            '54f2569620eca6a6ad9e628a523a44bcddf96d863b749354f8bc5dd324dabeac'
            '98e61ea711a8356927d105b3e8ce0d65fb8fa62b8cfbcbc91d8d9b13f285d74b'
            '396ff56fc87a48f80fcad4f83253f61285f4394e08bec24be7a3249f26585e33'
            'ecc09cdca976fd98eb50f94c20f98cf92dbb7efb57657a2619c4dec1a4effdaf'
            'a1a2e9aa9950e4438576291518c649074b6a0c473429bc16494995a981de614b'
            '77139f37224bc9921e05394909ec4c698af3089d97d0293ec4cd3aade252858a'
            '1703759a4e003414ae0f2856731f2da029d8be5995745768d6f260d60da7399e'
            '8be734582b9980ad0a3c5a82c200767ce558084f18e3cca11947991a14b0d68a'
            '06e2c8d66ebea68e8da1584c9e3f7a6dd97619ebf32d53c5ea611164a45c7f8a'
            'c39bfb8c5e4f6d26601e9cb03b979265ff0bbbcb2d2758005a4d0b0ed9616c7f'
            '37927b5a4e93461343841c84bdd1c67755bd03ac14d454ad8f4a952c37049ff7'
            'f9d9d4774e8714d7880cee022ba81db969469528263e7279a0f150f276fb47f8'
            '7c2df0ecdb825b0e6c7899139372ff3ebef0244241ce8dec216c8bbbb09c82a0'
            '16b560363717357a0e69d3b46269cace9bf978126e08b604ad18a532a8dfd0b2'
            'a48e49b9e6034ccd3b448bc44f4f30a4d64682e8820f68d470069c9cbcf8b6c7'
            '439c4d0978e3690f42a2c7b84a9fa91ed9b27979f244d4e8edd1461fe20d7011'
            '70d0cfa0ca9f7a948b248174ab516867f31e76361b7aabb70de05688ee78f92c'
            '17d5fb1c4ddb9e95da590d2e43ae3f7ce2b02c3ec698b16ed2752e3b3e7840c0'
            '499a9ec9a1aa7c5ef26ce5bb26f48576cccf1fc5906e966f92e25506405e9ad0')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  rm -rf scripts/addons && mv "${srcdir}"/blender-addons scripts/addons

  rm -f build_files/cmake/platform/platform_win32_mingw64.cmake
  rm -f release/windows/icons/blender.exe.manifest

  apply_patch_with_msg \
    0001-mingw-buildsystem.patch \
    0002-fix-mingw-w64-definitions.patch \
    0003-disable-static_assert_align-on-mingw.patch \
    0004-fix-build-with-openpgl-0.6.patch \
    0005-fix-WINTAB_PRINTF-on-mingw.patch \
    0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch \
    0007-disable-compiler-warnings.patch \
    0008-mingw-use-X11-like-path-layout.patch \
    0009-include-missing-headers.patch \
    0010-llvm-no-longer-needed.patch \
    0011-no-TIME_UTC-on-msvcrt.patch \
    0012-fix-build-with-OSL-1.13.patch \
    0013-force-draco.patch \
    0014-fix-find-numpy-2.patch \
    0015-mingw-python-like-msvc.patch \
    0016-mingw-windres.patch \
    0017-fix-build-with-python-3.12.patch \
    0018-fix-work-with-openvdb-11.patch \
    0019-fix-casts.patch \
    0020-blendthumb-build.patch \
    0021-fix-build-with-openvdb-12.patch \
    0022-find-llvm-convert-paths.patch \
    0023-link-errors.patch \
    0024-openvdb-shared.patch \
    0025-callback-function-prototype-fix.patch \
    0028-use-std-namespace.patch \
    0032-fix-finding-tbb.patch \
    0033-dont-add-fno-rtti-compile-option.patch \
    0034-Fix-ARM64-Build.patch \
    0035-Enable-SSE2NEON-on-Windows-ARM.patch \
    0039-no-WRL-on-mingw-w64.patch

  # https://projects.blender.org/blender/blender/pulls/121960
  # https://projects.blender.org/blender/blender/pulls/121947
  apply_patch_with_msg \
    ffmpeg-7-1.patch \
    ffmpeg-7-2.patch

  # Remove *.orig files
  find . -type f -name "*.orig" -exec rm -f {} \;
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM}  && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -w"
  CFLAGS+=" -w"
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
    CFLAGS+=" -Wno-unreachable-code-generic-assoc"
  fi

  if [[ $MINGW_PACKAGE_PREFIX != *-clang-* ]]; then
    CFLAGS+=" -Wno-incompatible-pointer-types"
  fi

  if [[ $CARCH == "aarch64" ]]; then
    _extra_config+=("-DSSE2NEON_ROOT_DIR=${MINGW_PREFIX}/include/sse2neon")
  fi

  export XR_OPENXR_SDK_ROOT_DIR=${MINGW_PREFIX}
  export EMBREE_ROOT_DIR=${MINGW_PREFIX}

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DWITH_SYSTEM_AUDASPACE=OFF \
    -DWITH_SYSTEM_GFLAGS=OFF \
    -DWITH_SYSTEM_GLOG=OFF \
    -DWITH_SYSTEM_BULLET=OFF \
    -DWITH_SYSTEM_LZO=ON \
    -DWITH_SYSTEM_EIGEN3=ON \
    -DWITH_SYSTEM_FREETYPE=ON \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_NANOVDB=OFF \
    -DWITH_OPENIMAGEIO=ON \
    -DWITH_CYCLES=ON \
    -DWITH_CYCLES_STANDALONE=ON \
    -DWITH_CYCLES_STANDALONE_GUI=ON \
    -DWITH_CYCLES_CUDA_BINARIES=OFF \
    -DWITH_CYCLES_OSL=ON \
    -DWITH_MEM_JEMALLOC=OFF \
    -DWITH_MOD_OCEANSIM=ON \
    -DPYTHON_VERSION=${_pyver} \
    -DPYTHON_LIBPATH=${MINGW_PREFIX}/lib \
    -DPYTHON_LIBRARY=${MINGW_PREFIX}/lib/libpython${_pyver}.dll.a \
    -DPYTHON_INCLUDE_DIR=${MINGW_PREFIX}/include/python${_pyver} \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_PYTHON_INSTALL_NUMPY=OFF \
    -DWITH_OPENMP=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  cp -r bin "${pkgdir}"${MINGW_PREFIX}/
  rm "${pkgdir}"${MINGW_PREFIX}/bin/msgfmt.exe

  # Manually install draco bindings
  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}"/
  mv "${pkgdir}"${MINGW_PREFIX}/share/blender/4*/python/lib/* "${pkgdir}"${MINGW_PREFIX}/lib/
  rm -r "${pkgdir}"${MINGW_PREFIX}/share/blender/4*/python

  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q "${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver%.*}"
  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q \
    "${pkgdir}"${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/startup \
    "${pkgdir}"${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/modules \
    "${pkgdir}"${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/addons

  install -dm755 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  for file in "${srcdir}/${_realname}-${pkgver}/doc/license/"*; do
    install -Dm644 "${file}" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  done
}
